package com.sky.controller.admin;


import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品相关接口")
/**
 * 菜品管理
 * */
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    private final String Dish_KEY="dish_*";
    /**
     * 新增菜品
     * */
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品:{}",dishDTO);
        dishService.saveDishWithFlavor(dishDTO);
        //清除原来缓存在内存的该分类的菜品数据
        String key="dish_"+dishDTO.getCategoryId();
        cleanRedisCache(key);
        return Result.success();
    }
    /**
     * 分页查询菜品
     * @param dishPageQueryDTO
     * @Return
     * */
    @GetMapping("/page")
    public Result<PageResult> pageQuery(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询菜品:{}",dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }
    /**
     * 批量删除
     * @param ids
     * @Return
     * */
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids){
        log.info("批量删除菜品:{}",ids);
        dishService.deleteBatch(ids);
        //删除所有缓存数据
        cleanRedisCache(Dish_KEY);
        return Result.success();
    }
    /**
     * 根据id查询菜品
     * @param id
     * @Return
     * */
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("根据id查询菜品:{}",id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }
    /**
     * 修改菜品
     * @param dishDTO
     * @Return
     * */
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品:{}",dishDTO);
        dishService.update(dishDTO);
        //删除所有缓存数据
        cleanRedisCache(Dish_KEY);
        return Result.success();
    }
    /**
     * 商品起售、停售
     * @param status
     * @param id
     * @Return
     * */
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("商品起售、停售:{},{}",status,id);
        dishService.startOrStop(status,id);
        //删除所有缓存数据
        cleanRedisCache(Dish_KEY);
        return Result.success();
    }
    /**
     * 根据分类id查询菜品
     *
     * */
    @GetMapping("/list")
    public Result<List<DishVO>> list(Long categoryid){
        log.info("菜品id：{}",categoryid);
        Dish dish = new Dish();
        dish.setCategoryId(categoryid);
        dish.setStatus(StatusConstant.ENABLE);
        List<DishVO> list= dishService.listWithFlavor(dish);
        return Result.success(list);
    }
    /**
     * 清理redis缓存数据
     * */
    private void cleanRedisCache(String key){
        Set keys = redisTemplate.keys(key);
        redisTemplate.delete(keys);
    }

}
